// Filename: 64TO32.CPP

/************   Complex to real picture converter  ********************\

1993 (c) CDI UI, The USSR Academy of Sciences, Samara
Two modes: complex -> (real, imag)
           complex -> (module, phase)
            
\**********************************************************************/
#include <io.h>
#include <dos.h>
#include <string.h>
#include "fft.h"
#include "indicat.h"

int _64to32( char *ComplexPic, char *Dst1Pic, char *Dst2Pic, int Mode ){
char *dob[] =  
  { "ComplToReal{}", "ComplToImag{}", "ComplToModule{}", "ComplToPhase{}" };

if(Mode!=Real_Imag && Mode!=Mod_Arg) return InvalidParams;
int fsrc = open(ComplexPic,1);
if ( fsrc == -1 ) return NoFile;
int head[4]; _read(fsrc,head,8);
if(head[2]!=64) return WrongType;
int m=head[0], n=head[1], cl0=head[3];
if( (long)m*n*8+8+head[3] != filelength(fsrc) ) return InvalidPicture;
dfree freespace; getdfree(0,&freespace);

long fsp   = (long)freespace.df_avail*freespace.df_bsec*freespace.df_sclus;
long reqsp = (long)m*n*8+16+2*head[3]+30;
if(fsp < reqsp) return NoDiskSpace;
char *comm = new char[head[3]+20];
float *sf1 = new float[n], *sf2 = new float[n];
Complex *str_src = new Complex[n];
if ( !str_src) return NoMemory;

head[2]=32;
//*int cl1,cl2;
//*switch(Mode){
//*  case Real_Imag: cl1=strlen(dob[0]); cl2=strlen(dob[1]); break;
//*  case Mod_Arg:   cl1=strlen(dob[2]); cl2=strlen(dob[3]); break;
//*}
int fdest1 = open(Dst1Pic, 770, 128 ), fdest2 = open(Dst2Pic, 770, 128 );
//head[3]+=cl1; _write(fdest1,head,8);
//head[3]+=(cl2-cl1); _write(fdest2,head,8);
head[3]=0; _write(fdest1,head,8);
head[3]=0; _write(fdest2,head,8);

check_indicator Ind(m,"Complex-Real conversion");
int i;
if (Mode==Real_Imag) {
  for(i=0;i<m;i++) {
    _read(fsrc, str_src, n*8);
    for(int j=0;j<n;j++) { sf1[j]=str_src[j].re; sf2[j]=str_src[j].im; }
    _write( fdest1, sf1, 4*n);
    _write( fdest2, sf2, 4*n);
    Ind.update(i);
    if(Ind.check()) {
      close(fsrc); close(fdest1); close(fdest2);
      unlink(Dst1Pic); unlink(Dst2Pic);
      delete sf1; delete sf2; delete str_src; delete comm;
      return User_break;
    }
  }
}
else {
  for(i=0;i<m;i++) {
    _read(fsrc, str_src, n*8);
    for(int j=0;j<n;j++) {
      sf1[j]=hypot(str_src[j].re, str_src[j].im);
      if(sf1[j]==0.0) sf2[j]=0.0;
	else sf2[j]=atan2(str_src[j].im,str_src[j].re);
    }
    _write( fdest1, sf1, 4*n);
    _write( fdest2, sf2, 4*n);
    Ind.update(i);
    if(Ind.check()){
      close(fsrc); close(fdest1); close(fdest2);
      unlink(Dst1Pic); unlink(Dst2Pic);
      delete sf1; delete sf2; delete str_src; delete comm;
      return User_break;
    }
  }
}
delete sf1; delete sf2; delete str_src;

_read( fsrc, comm, cl0 ); close(fsrc);
comm[cl0]=0;
switch(Mode) {
  case Real_Imag: strcat(comm,dob[0]); break;
  case Mod_Arg:   strcat(comm,dob[2]); break;
}
//_write( fdest1, comm, cl0+cl1);
comm[cl0]=0;
switch(Mode) {
  case Real_Imag: strcat(comm,dob[1]); break;
  case Mod_Arg:   strcat(comm,dob[3]); break;
}
//_write( fdest2, comm, cl0+cl2);
close(fdest1); close(fdest2);

delete comm;
return 0;
}

